package com.compass.doctor.ui.home

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import com.compass.doctor.databinding.ActivityKnowledgeCenterBinding
import com.compass.doctor.ui.home.view.ArticleAndVideoView
import com.compass.doctor.ui.home.view.LatestBuyerView
import com.compass.doctor.ui.home.view.LatestCommentsView
import com.compass.doctor.ui.home.viewmodel.KnowledgeCenterViewModel
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.ext.gone
import com.compass.framework.ext.visible
import com.compass.framework.model.KnowledgeCenter
import com.compass.framework.utils.BigDecimalUtils
import com.compass.framework.utils.StatusBarSettingHelper

/**
 * Description： 知识中心
 * Date:2023/12/23 21:15
 */
class KnowledgeCenterActivity : BaseVBActivity<ActivityKnowledgeCenterBinding>() {


    companion object {
        fun start(context: Context) {
            val intent = Intent(context, KnowledgeCenterActivity::class.java)
            context.startActivity(intent)
        }
    }

    private val knowledgeCenterViewModel: KnowledgeCenterViewModel by viewModels()
    private lateinit var latestCommentsView: LatestCommentsView
    private lateinit var latestBuyerView: LatestBuyerView
    private lateinit var articleAndVideoView: ArticleAndVideoView
    override fun initView(savedInstanceState: Bundle?) {
        StatusBarSettingHelper.setStatusBarTranslucent(this)
        StatusBarSettingHelper.statusBarLightMode(this@KnowledgeCenterActivity, true)
        latestCommentsView = LatestCommentsView(this)
        latestBuyerView = LatestBuyerView(this)
        articleAndVideoView = ArticleAndVideoView(this)

    }

    override fun initData() {
        showLoading()
        knowledgeCenterViewModel.getKnowledgeInfo()
        knowledgeCenterViewModel.knowledgeLiveData.observe(this) {
            dismissLoading()
            it?.let { knowledge->
                setTopData(knowledge)
                if (knowledge.commentList.isNotEmpty()){
                    mBinding.flyComment.addView(latestCommentsView)
                    latestCommentsView.initData(knowledge.commentList)
                }
                if (knowledge.orderSpecialColumnList.isNotEmpty()){
                    mBinding.flyLatestBuyer.addView(latestBuyerView)
                    latestBuyerView.initData(knowledge.orderSpecialColumnList)
                }
                if (knowledge.articleList.isNotEmpty()||knowledge.videoList.isNotEmpty()){
                    mBinding.flyArticleVideo.addView(articleAndVideoView)
                    articleAndVideoView.initData(knowledge.articleList,knowledge.videoList)
                }
              if (knowledge.commentList.isEmpty()&&knowledge.orderSpecialColumnList.isEmpty()&&knowledge.articleList.isEmpty())      mBinding.emptyView.visible() else      mBinding.emptyView.gone()

            }

        }
    }

    private fun setTopData(knowledge: KnowledgeCenter) {
        mBinding.apply {
            tvArticleCount.text = knowledge.articleCount.toString()
            tvVideoCount.text = knowledge.videoCount.toString()
            tvSubscribers.text = knowledge.specialColumnCount.toString()
            tvMoney.text = BigDecimalUtils.divide(knowledge.specialColumnJournalismCountPrice)
        }
    }


}